package com.chhuang.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @ClassName Swagger3Config
 * @Description TODO
 * @Author Darren Huang
 * @Date 2022/11/19 9:31
 * @Version 1.0
 */
@Configuration
public class SwaggerConfig {

    @Value("${swagger.enable}")
    private Boolean enable;

    @Bean
    public Docket createRestApi(){
//        return new Docket(DocumentationType.OAS_30)//swagger3使用OAS_30
        return new Docket(DocumentationType.SWAGGER_2)//swagger2使用SWAGGER_2
                .apiInfo(apiInfo())
                .enable(enable)
                .select()
                //设置通过什么方式定位到需要生成文档的接口，通过@ApiOperation定位方法
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //接口URL路径，any表示全部路径，有要求可以用正则做匹配如：.regex("/api/*")
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("chhuangboot平台API接口文档")
                .description("chhuangboot平台API接口功能、参数与返回值的描述，以及接口的调试")
                .contact(new Contact("Darren Huang",
                        "https://cn.bing.com/",
                        "princechen91@foxmail.coms"))
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }
}
